#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import datetime
from django.utils import simplejson

from model import Topic
from model import Lecture

class MyHandler(webapp.RequestHandler):
    
    def get(self):
        voiceCount = ();

        lectureKey=self.request.get('lectureKey')
        lecture=Lecture.get(lectureKey)
        topics = db.Query(Topic).filter('lecture =', lecture).order('index')

        topicsobj = []
#        json = "["

        for t in topics:
            obj = { "name": t.name, "index":str(t.index), "key": str(t.key()), "time":str(t.time)}
            topicsobj.append(obj)
#            json += "{ name:" + t.name + ", index: " + str(t.index) + ", time: " + str(t.time) + "},"


#        if len(json) > 1:
#            json = json[:-1]

#        json += "]"
        self.response.out.write(simplejson.dumps(topicsobj))

def main():
    app = webapp.WSGIApplication([
        (r'.*', MyHandler)], debug=True)
    wsgiref.handlers.CGIHandler().run(app)

if __name__ == "__main__":
    main()